﻿using MediatR;
using SmartTracking.API.Write.Application.Commands.SheepFarm_command;
using SmartTracking.Domain.Sheep;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.SheepFarm_handler
{
    public class DeleteSheepFarmCommandHandler:IRequestHandler<DeleteSheepFarmCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<SheepFarm> sheepFarmRep;

        public DeleteSheepFarmCommandHandler(
            IBaseRepository<SheepFarm> sheepFarmRep)
        {
            this.sheepFarmRep = sheepFarmRep;
        }

        public async Task<ApiResult<int>> Handle(DeleteSheepFarmCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Message = ApiEnum.删除成功.ToString();

            var list = sheepFarmRep.GetAll().FirstOrDefault(x => x.FarmId == request.FarmId);
            list.IsDeleted = true;
            list.DeleteTime = DateTime.Now;
            var res = await sheepFarmRep.UpdateAsync(list);

            result.data = res;
            return await Task.FromResult(result);
        }
    }
}
